package ca.compsci.opent.doctool;

/**
 * Defines the types that need to be supported for OpenT.
 * 
 * @since 0.0.2
 */
public enum Type
{
	/**
	 * Models OpenT boolean type
	 * 
	 * @since 0.0.2
	 */
	BOOLEAN(Boolean.class),
	
	/**
	 * Models OpenT char type
	 * 
	 * @since 0.0.2
	 */
	CHAR(Character.class),
	
	/**
	 * Models OpenT int type
	 * 
	 * @since 0.0.2
	 */
	INT(Integer.class),
	
	/**
	 * Models OpenT real type
	 * 
	 * @since 0.0.2
	 */
	REAL(Double.class),
	
	/**
	 * Models OpenT string type
	 * 
	 * @since 0.0.2
	 */
	STRING(String.class);
	
	/**
	 * Corresponding java class
	 * 
	 * @since 0.0.2
	 */
	private Class jClass;
	
	/**
	 * Name of the type
	 * 
	 * @since 0.0.2
	 */
	private String name;
	
	/**
	 * Creates type with corresponding Java class
	 * 
	 * @param jClass Java class that corresponds to type
	 * 
	 * @since 0.0.2
	 */
	Type(Class jClass)
	{
		this.jClass = jClass;
	}
	
	/**
	 * Gets the types corresponding Java class.
	 * 
	 * @return corresponding Java class
	 * 
	 * @since 0.0.2
	 */
	public Class getJClass()
	{
		return jClass;
	}
	
	/**
	 * Returns the {@code Type} that matches the specified string.
	 * 
	 * @param str string to match
	 * 
	 * @return {@code Type} that matches {@code str}
	 * 
	 * @throws OpenTDocException if {@code str} doesn't match a valid type
	 * 
	 * @since 0.0.3
	 */
	public static Type getType(String str) throws OpenTDocException
	{
		// TODO: test for other user-defined types
		
		try
		{
			return Type.valueOf(str.toUpperCase());
		}
		catch (IllegalArgumentException e)
		{
			throw new OpenTDocException("Type does not exist - " + str);
		}
	}
	
	/**
	 * Returns the name of this type as it appears in OpenT
	 * 
	 * @return name of this type
	 * 
	 * @since 0.0.3
	 */
	@Override
	public String toString()
	{
		return super.toString().toLowerCase();
	}
}
